﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json.Linq;
using Wala.Infrastructure;

namespace Wala.Connection.Objects.Timeline
{
    public class PhotoInfo:TimelineInfo
    {
        #region //--------- Private Fields --------------//
        private long _photo_id;
        private string _link;
        private string _thumbnail_link;
        private string _caption;
        private long _album_id = 0;
        private string _album_title;
        private long _category_id = 0;
        private string _category_title;
        private List<Privacy> _privacy;

        

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region //--------- Public Constructors ---------//
        /// <summary>
        /// Initializes a new instance of the <see cref="PhotoInfo" /> class.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <param name="ownerId">The owner id.</param>
        /// <param name="link">The link.</param>
        /// <param name="thumbnailLink">The thumbnail link.</param>
        /// <param name="caption">The caption.</param>
        /// <param name="locationText">The location text.</param>
        /// <param name="album">The album.</param>
        /// <param name="albumTitle">The album title.</param>
        /// <param name="categoryId">The category id.</param>
        /// <param name="categoryTitle">The category title.</param>
        /// <param name="timeStamp">The time stamp.</param>
        /// <param name="locationX">The location X.</param>
        /// <param name="locationY">The location Y.</param>
        /// <param name="totalComments">The total comments.</param>
        /// <param name="totalLikes">The total likes.</param>
        /// <param name="totalShares">The total shares.</param>
        /// <param name="ownerDisplayName">Display name of the owner.</param>
        /// <param name="ownerAvatarId">The owner avatar id.</param>
        /// <param name="ownerCoverId">The owner cover id.</param>
        /// <param name="isUserLike">The is user like.</param>
        /// <param name="isUserTag">The is user tag.</param>
        /// <param name="isUserFollow">The is user follow.</param>
        public PhotoInfo(long id, int ownerId, string link, string thumbnailLink, string caption,
            string locationText, long album, string albumTitle, long categoryId, string categoryTitle,
            long timeStamp, double locationX, double locationY, int totalComments, int totalLikes,
            int totalShares, string ownerDisplayName, long ownerAvatarId, long ownerCoverId,
            bool isUserLike, bool isUserTag, bool isUserFollow)
            : base(ownerId, ownerAvatarId, ownerCoverId, ownerDisplayName, locationText, locationX, locationY,
                totalComments, totalLikes, totalShares, timeStamp, isUserLike, isUserTag, isUserFollow)
        {
            _photo_id = id;
            _category_title = categoryTitle;
            _category_id = categoryId;

            _album_title = albumTitle;
            _album_id = album;

            _caption = caption;

            _thumbnail_link = thumbnailLink;
            _link = link;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PhotoInfo" /> class.
        /// </summary>
        public PhotoInfo()
        {
        }

        #endregion

        #region //--------- CLR Properties --------------//



        /// <summary>
        /// Gets the type.
        /// </summary>
        /// <value>The type.</value>
        public override int Type
        {
            get
            {
                return URLConstant.TargetType.PHOTO;
            }
        }

        public long Photo_Id
        {
            get { return _photo_id; }
            set { _photo_id = value; }
        }

        /// <summary>
        /// Gets or sets the category title.
        /// </summary>
        /// <value>The category title.</value>
        public string Category_Title
        {
            get
            {
                return _category_title;
            }
            set
            {

                if (_category_title != value)
                {
                    _category_title = value;
                    OnPropertyChanged("Category_Title");
                }

            }
        }

        /// <summary>
        /// Gets or sets the category id.
        /// </summary>
        /// <value>The category id.</value>
        public long Category_Id
        {
            get
            {
                return _category_id;
            }
            set
            {

                if (_category_id != value)
                {
                    _category_id = value;
                    OnPropertyChanged("Category_Id");
                }

            }
        }

        /// <summary>
        /// Gets or sets the album title.
        /// </summary>
        /// <value>The album title.</value>
        public string Album_Title
        {
            get
            {
                return _album_title;
            }
            set
            {

                if (_album_title != value)
                {
                    _album_title = value;
                    OnPropertyChanged("Album_Title");
                }

            }
        }

        /// <summary>
        /// Gets or sets the album id.
        /// </summary>
        /// <value>The album id.</value>
        public long Album_Id
        {
            get
            {
                return _album_id;
            }
            set
            {

                if (_album_id != value)
                {
                    _album_id = value;
                    OnPropertyChanged("Album_Id");
                }

            }
        }

        /// <summary>
        /// Gets or sets the caption.
        /// </summary>
        /// <value>The caption.</value>
        public string Caption
        {
            get
            {
                return _caption;
            }
            set
            {
                if (_caption != value)
                {
                    _caption = value;
                    OnPropertyChanged("Caption");
                }
            }
        }

        /// <summary>
        /// Gets or sets the thumbnail link.
        /// </summary>
        /// <value>The thumbnail link.</value>
        public string Thumbnail_Link
        {
            get
            {
                return _thumbnail_link;
            }
            set
            {

                if (_thumbnail_link != value)
                {
                    _thumbnail_link = value;
                    OnPropertyChanged("Thumbnail_Link");
                }

            }
        }

        /// <summary>
        /// Gets or sets the link.
        /// </summary>
        /// <value>The link.</value>
        public string Link
        {
            get
            {
                return _link;
            }
            set
            {
                if (_link != value)
                {
                    _link = value;
                    OnPropertyChanged("Link");
                }
            }
        }

        /// <summary>
        /// Gets the privacy.
        /// </summary>
        /// <value>The privacy.</value>
        public List<Privacy> Privacy
        {
            get { return _privacy; }
            set { _privacy = value; }
        }

        #endregion

        #region //--------- Static Methods --------------//
        /// <summary>
        /// Get a PhotoInfo object from JSON string.
        /// </summary>
        /// <param name="json">JSON string.</param>
        /// <returns>A PhotoInfo object.</returns>
        public static PhotoInfo FromJson(string json)
        {
            try
            {
                JObject jObject = JObject.Parse(json);

                PhotoInfo result = new PhotoInfo()
                {

                    Photo_Id = (long)jObject["photo_id"],
                    Owner_Id = (int)jObject["owner_id"],

                    Link = (string)jObject["link"],
                    Thumbnail_Link = (string)jObject["thumbnail_link"],

                    Caption = (string)jObject["caption"],
                    Time_Stamp = (long)jObject["time_stamp"],

                    Location_Text = (string)jObject["location_text"],
                    Location_X = (double)jObject["location_x"],
                    Location_Y = (double)jObject["location_y"],

                    Album_Id = (long)jObject["album_id"],
                    Album_Title = (string)jObject["album_title"],

                    Category_Id = (long)jObject["category_id"],
                    Category_Title = (string)jObject["category_title"],

                    Number_Of_Comments = (int)jObject["number_of_comments"],
                    Number_Of_Likes = (int)jObject["number_of_likes"],
                    Number_Of_Shares = (int)jObject["number_of_shares"],

                    Owner_Display_Name = (string)jObject["owner_display_name"],
                    Owner_Avatar_Id = (long)jObject["owner_avatar_id"],
                    Owner_Cover_Id = (long)jObject["owner_cover_id"],

                    User_Like = (bool)jObject["user_like"],
                    User_Tag = (bool)jObject["user_tag"],
                    User_Follow = (bool)jObject["user_follow"]
                };

                return result;
            }
            catch (Exception)
            {
                return new PhotoInfo();
            }
        }
        #endregion

        #region //--------- Abstract Class Members ------//

        public override long Id
        {
            get
            {
                return this.Photo_Id;
            }
            set
            {
                this.Photo_Id = value;
                OnPropertyChanged("ID");
            }
        }
                
        #endregion

        #region //----------- Private Methods ---------//

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
